package com.demo.service.impl;

import com.demo.dao.UserRepository;
import com.demo.pojo.User;
import com.demo.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Random;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author Windy
 * @Date 2022/9/16 21:21
 * @Version 1.0
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserRepository userRepository;

    /**
     *  邮件发送对象
     */
    @Autowired
    JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;
    @Override
    public boolean login(User user) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getName(), user.getPwd());

        try {
            subject.login(token);
        } catch (UnknownAccountException ue){
            System.out.println("账号："+user.getName()+"不存在");
        }catch(IncorrectCredentialsException ice){
            System.out.println("不正确的用户凭证！");
        }catch (AuthenticationException e) {
            e.printStackTrace();
        }

        System.out.println(token);
        System.out.println(subject.isAuthenticated());
        if (subject.isAuthenticated()){
            return true;
        }
        return false;
    }



    @Override
    public String sender_code(User user, HttpSession session) {
        User byMail = userRepository.findByMail(user.getMail());
        if(byMail != null){
            return "邮箱已存在";
        }

        //声明邮件客户端
        SimpleMailMessage message = new SimpleMailMessage();
        //声明发件人
        message.setFrom(from);
        //声明收件人
        message.setTo(user.getMail());
        //声明发送主题
        message.setSubject("验证码");
        //声明主体
        StringBuffer stringBuffer = new StringBuffer();
        for (int i=0;i<4;i++){
            int i1 = new Random().nextInt(10);
            stringBuffer.append(i1);
        }
        message.setText(stringBuffer.toString());

        //发送邮件
        javaMailSender.send(message);

        //将验证信息存储在session中
        session.setAttribute("code",stringBuffer.toString());
        return "发送成功";
    }

    @Override
    public String register(User user, HttpSession session) {
        boolean b = session.getAttribute("code").equals(user.getCode());
        if (!b) {
            return "验证码错误！";
        }
        User byName = userRepository.findByName(user.getName());
        if (byName != null) {
            return "用户名已存在！";
        }
        String pwd = new Md5Hash(user.getPwd(), ByteSource.Util.bytes(user.getName()), 1024).toString();
        user.setPwd(pwd);
        userRepository.save(user);
        return "注册成功";
    }
}
